package com.ymatou.iapi.fenqileordersync.testcase;

import java.math.BigDecimal;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.fenqileordersync.service.FenqileOrderBaseService;
import com.ymatou.iapi.optrade.parameter.enums.CancelOrderTypeEnum;
import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.parameter.req.RollbackToWaitDeliveryReq;
import com.ymatou.iapi.optrade.resp.Order;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.RefundService;
import com.ymatou.iapi.optrade.service.RollbackToWaitDeliveryCall;
import com.ymatou.iapi.synctrade.parameter.TradingSyncCmdEnum;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.database.model.fqlorder.FqlSyncOrderDetail;
import com.ymttest.database.model.thirdpartysync.fenqilesku;
import com.ymttest.database.sharding.db.sharding.model.OrderDetailPo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sqlwapper.fqlorder.FqlOrderInfoWrapper;
import com.ymttest.database.sqlwapper.fqlorder.FqlSyncOrderDetailWrapper;
import com.ymttest.utils.TestDataManager;

public class Ts_FenqileOrderSync {

	private static FqlOrderInfoWrapper fqlOrderInfoWrapper = new FqlOrderInfoWrapper();
	private static FqlSyncOrderDetailWrapper fqlSyncOrderDetailWrapper = new FqlSyncOrderDetailWrapper();

	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("同步指令接口");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult("api/FenqileOrderSync");
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * 
	 * @param orderPo
	 * @param actualSyncOrderDetail
	 * @param isSuccess
	 *            （下单是否成功）
	 * @param orderStatus
	 * @param isRefund
	 * @param isDeliveryRollbacked
	 */
	public void verifyDB(OrderPo orderPo,
			FqlSyncOrderDetail actualSyncOrderDetail, int orderStatus,
			Boolean isRefund, Boolean isDeliveryRollbacked) {

		Logger.verifyEquals(4, orderPo.getOrderType(), "验证分期乐订单类型");
		Logger.verifyEquals(orderStatus,
				(int) actualSyncOrderDetail.getOrder_status(), "验证orderStatus");

		if (isRefund)
			Logger.verifyEquals(1, actualSyncOrderDetail.getHas_refund(),
					"验证has_refund");
		else
			Logger.verifyEquals(null, actualSyncOrderDetail.getHas_refund(),
					"验证has_refund");

		if (isDeliveryRollbacked)
			Logger.verifyEquals(isDeliveryRollbacked,
					actualSyncOrderDetail.getHas_dispatch_rollbacked(),
					"验证has_dispatch_rollbacked");
		else
			Logger.verifyEquals(null,
					actualSyncOrderDetail.getHas_dispatch_rollbacked(),
					"验证has_dispatch_rollbacked");
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_FenqileOrderSync_001() {
		Logger.start(true, "生成分期乐订单,验证同步数据");
		try {

			String ymtProductId = FenqileOrderBaseService
					.createFenqileProduct(false).getProductIdList().get(0);

			fenqilesku fenqilesku = FenqileOrderBaseService
					.getFenqileSkuId(ymtProductId);

			String thirdSkuId = fenqilesku.getYmtSkuId();
			String skuId = fenqilesku.getFqlSkuId();
			fenqilesku.getYmtPrice();

			String fqlOrderId = TestDataManager.randomStr(10);
			BigDecimal amount = fenqilesku.getYmtPrice().add(
					new BigDecimal("10.00"));

			int skuNum = 2;

			fqlOrderInfoWrapper.insert(fqlOrderId, thirdSkuId, amount, skuNum,
					skuId);
			fqlSyncOrderDetailWrapper.insert(fqlOrderId, 2);

			Logger.comment(skuId);

			FenqileOrderBaseService.generateYmatouOrder();

			FqlSyncOrderDetail actualSyncOrderDetail = fqlSyncOrderDetailWrapper
					.selectByFqlOrderId(fqlOrderId);

			long orderId = actualSyncOrderDetail.getOrder_id();

			OrderPo orderPo = orderShardingWapper.selectOrderbyOrderId(orderId);

			Logger.verifyEquals("fql_" + fqlOrderId, orderShardingWapper
					.selectMianOrderbyId(orderPo.getMainOrderId())
					.getThirdMallOrderId(), "验证ThirdMallOrderId");

			this.verifyDB(orderPo, actualSyncOrderDetail, 2, false, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_FenqileOrderSync_002() {
		Logger.start(true, "生成分期乐订单并取消，验证同步数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			String ymtProductId = FenqileOrderBaseService
					.createFenqileProduct(false).getProductIdList().get(0);

			fenqilesku fenqilesku = FenqileOrderBaseService
					.getFenqileSkuId(ymtProductId);

			String thirdSkuId = fenqilesku.getYmtSkuId();
			String skuId = fenqilesku.getFqlSkuId();
			fenqilesku.getYmtPrice();

			String fqlOrderId = TestDataManager.randomStr(10);
			BigDecimal amount = fenqilesku.getYmtPrice().add(
					new BigDecimal("10.00"));

			int skuNum = 2;

			fqlOrderInfoWrapper.insert(fqlOrderId, thirdSkuId, amount, skuNum,
					skuId);
			fqlSyncOrderDetailWrapper.insert(fqlOrderId, 2);

			Logger.comment(skuId);

			FenqileOrderBaseService.generateYmatouOrder();

			Logger.comment("前置数据准备结束");

			FqlSyncOrderDetail actualSyncOrderDetail = fqlSyncOrderDetailWrapper
					.selectByFqlOrderId(fqlOrderId);

			long orderId = actualSyncOrderDetail.getOrder_id();

			OrderPo orderPo = orderShardingWapper.selectOrderbyOrderId(orderId);

			TradingCallService.cancelOrder(orderId,
					CancelOrderTypeEnum.SELLER_CANCEL_ORDER, 24635724);

			actualSyncOrderDetail = fqlSyncOrderDetailWrapper
					.selectByFqlOrderId(fqlOrderId);

			this.verifyDB(orderPo, actualSyncOrderDetail, 13, false, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_FenqileOrderSync_003() {
		Logger.start(true, "生成分期乐订单并接单，验证同步数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			String ymtProductId = FenqileOrderBaseService
					.createFenqileProduct(false).getProductIdList().get(0);

			fenqilesku fenqilesku = FenqileOrderBaseService
					.getFenqileSkuId(ymtProductId);

			String thirdSkuId = fenqilesku.getYmtSkuId();
			String skuId = fenqilesku.getFqlSkuId();
			fenqilesku.getYmtPrice();

			String fqlOrderId = TestDataManager.randomStr(10);
			BigDecimal amount = fenqilesku.getYmtPrice().add(
					new BigDecimal("10.00"));

			int skuNum = 2;

			fqlOrderInfoWrapper.insert(fqlOrderId, thirdSkuId, amount, skuNum,
					skuId);
			fqlSyncOrderDetailWrapper.insert(fqlOrderId, 2);

			Logger.comment(skuId);

			FenqileOrderBaseService.generateYmatouOrder();

			Logger.comment("前置数据准备结束");

			FqlSyncOrderDetail actualSyncOrderDetail = fqlSyncOrderDetailWrapper
					.selectByFqlOrderId(fqlOrderId);

			long orderId = actualSyncOrderDetail.getOrder_id();

			OrderPo orderPo = orderShardingWapper.selectOrderbyOrderId(orderId);

			TradingCallService.sellerAcceptOrder(24635724, orderId);

			Thread.sleep(3000);

			FenqileOrderBaseService.onOrderAccept(orderId);

			actualSyncOrderDetail = fqlSyncOrderDetailWrapper
					.selectByFqlOrderId(fqlOrderId);

			this.verifyDB(orderPo, actualSyncOrderDetail, 17, false, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_FenqileOrderSync_004() {
		Logger.start(true, "生成分期乐订单并发货回退到待发货状态，验证同步数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			String ymtProductId = FenqileOrderBaseService
					.createFenqileProduct(false).getProductIdList().get(0);

			fenqilesku fenqilesku = FenqileOrderBaseService
					.getFenqileSkuId(ymtProductId);

			String thirdSkuId = fenqilesku.getYmtSkuId();
			String skuId = fenqilesku.getFqlSkuId();
			fenqilesku.getYmtPrice();

			String fqlOrderId = TestDataManager.randomStr(10);
			BigDecimal amount = fenqilesku.getYmtPrice().add(
					new BigDecimal("10.00"));

			int skuNum = 2;

			fqlOrderInfoWrapper.insert(fqlOrderId, thirdSkuId, amount, skuNum,
					skuId);
			fqlSyncOrderDetailWrapper.insert(fqlOrderId, 2);

			Logger.comment(skuId);

			FenqileOrderBaseService.generateYmatouOrder();

			Logger.comment("前置数据准备结束");

			FqlSyncOrderDetail actualSyncOrderDetail = fqlSyncOrderDetailWrapper
					.selectByFqlOrderId(fqlOrderId);

			long orderId = actualSyncOrderDetail.getOrder_id();

			OrderPo orderPo = orderShardingWapper.selectOrderbyOrderId(orderId);

			TradingCallService.sellerAcceptOrder(24635724, orderId);

			TradingCallService.delivery(24635724L, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);

			Logger.comment("前置数据准备结束");

			Thread.sleep(5000);

			String operateUser = "sys";
			String reason = "autotest rollback";

			RollbackToWaitDeliveryReq rollbackToWaitDeliveryReq = new RollbackToWaitDeliveryReq();
			RollbackToWaitDeliveryCall rollbacktowaitdeliveryCall = new RollbackToWaitDeliveryCall();

			rollbackToWaitDeliveryReq.setOrderId(orderId);
			rollbackToWaitDeliveryReq.setOperateUser(operateUser);
			rollbackToWaitDeliveryReq.setReason(reason);
			rollbacktowaitdeliveryCall
					.setData(false, rollbackToWaitDeliveryReq);

			rollbacktowaitdeliveryCall.callService();

			TradingSyncService.syncOrderCmd(24117999, orderId,
					TradingSyncCmdEnum.LOGISTICS_CODE);

			Thread.sleep(5000);

			actualSyncOrderDetail = fqlSyncOrderDetailWrapper
					.selectByFqlOrderId(fqlOrderId);

			this.verifyDB(orderPo, actualSyncOrderDetail, 17, false, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_FenqileOrderSync_005() {
		Logger.start(true, "生成分期乐订单并部分退货退款，验证同步数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			String fqlOrderId = TestDataManager.randomStr(10);
			int skuNum = 2;
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeFenqileOrder(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			Order order = placeOrderResp.getOrderList().get(0);
			Long orderId = order.getOrderId();

			// 支付
			TradingCallService.payOrder(placeOrderResp.getUserId(),
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());

			fqlOrderInfoWrapper.insert(fqlOrderId, "autotestthirdSkuId",
					BigDecimal.ONE, skuNum, "autotestskuid");
			fqlSyncOrderDetailWrapper.insert(fqlOrderId, 2, orderId);

			// 接单
			TradingCallService.sellerAcceptOrder(order.getSellerId(), orderId);

			// 退款
			RefundService.approveRefund(order, 1);

			FqlSyncOrderDetail actualSyncOrderDetail = fqlSyncOrderDetailWrapper
					.selectByFqlOrderId(fqlOrderId);

			OrderPo orderPo = orderShardingWapper.selectOrderbyOrderId(orderId);

			Logger.comment("前置数据准备结束");

			Thread.sleep(5000);

			actualSyncOrderDetail = fqlSyncOrderDetailWrapper
					.selectByFqlOrderId(fqlOrderId);

			this.verifyDB(orderPo, actualSyncOrderDetail, 17, true, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_FenqileOrderSync_006() {
		Logger.start(true, "生成分期乐订单并部分退货退款，验证同步数据");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			String fqlOrderId = TestDataManager.randomStr(10);
			int skuNum = 2;
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeFenqileOrder(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			Order order = placeOrderResp.getOrderList().get(0);
			Long orderId = order.getOrderId();

			// 支付
			TradingCallService.payOrder(placeOrderResp.getUserId(),
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());

			fqlOrderInfoWrapper.insert(fqlOrderId, "autotestthirdSkuId",
					BigDecimal.ONE, skuNum, "autotestskuid");
			fqlSyncOrderDetailWrapper.insert(fqlOrderId, 2, orderId);

			// 接单
			TradingCallService.sellerAcceptOrder(order.getSellerId(), orderId);

			// 退款
			RefundService.approveRefund(order);

			FqlSyncOrderDetail actualSyncOrderDetail = fqlSyncOrderDetailWrapper
					.selectByFqlOrderId(fqlOrderId);

			OrderPo orderPo = orderShardingWapper.selectOrderbyOrderId(orderId);

			Logger.comment("前置数据准备结束");

			Thread.sleep(5000);

			actualSyncOrderDetail = fqlSyncOrderDetailWrapper
					.selectByFqlOrderId(fqlOrderId);

			this.verifyDB(orderPo, actualSyncOrderDetail, 12, true, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_FenqileOrderSync_007() {
		Logger.start(true, "库存不足，生成分期乐订单失败,验证同步数据");
		try {

			String ymtProductId = FenqileOrderBaseService
					.createFenqileProduct(false).getProductIdList().get(0);

			fenqilesku fenqilesku = FenqileOrderBaseService
					.getFenqileSkuId(ymtProductId);

			String thirdSkuId = fenqilesku.getYmtSkuId();
			String skuId = fenqilesku.getFqlSkuId();
			fenqilesku.getYmtPrice();

			String fqlOrderId = TestDataManager.randomStr(10);
			BigDecimal amount = fenqilesku.getYmtPrice().add(
					new BigDecimal("10.00"));

			int skuNum = 200000;

			fqlOrderInfoWrapper.insert(fqlOrderId, thirdSkuId, amount, skuNum,
					skuId);
			fqlSyncOrderDetailWrapper.insert(fqlOrderId, 2);

			Logger.comment(skuId);

			FenqileOrderBaseService.generateYmatouOrder();

			FqlSyncOrderDetail actualSyncOrderDetail = fqlSyncOrderDetailWrapper
					.selectByFqlOrderId(fqlOrderId);

			Long orderId = actualSyncOrderDetail.getOrder_id();

			Logger.verifyEquals(null, orderId, "验证ThirdMallOrderId");
			Logger.verifyEquals("库存不足", actualSyncOrderDetail.getRemark(),
					"验证remark");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_FenqileOrderSync_008() {
		Logger.start(true, "分期乐价格小于码头价格，生成分期乐订单失败,验证同步数据");
		try {

			String ymtProductId = FenqileOrderBaseService
					.createFenqileProduct(false).getProductIdList().get(0);

			fenqilesku fenqilesku = FenqileOrderBaseService
					.getFenqileSkuId(ymtProductId);

			String thirdSkuId = fenqilesku.getYmtSkuId();
			String skuId = fenqilesku.getFqlSkuId();
			fenqilesku.getYmtPrice();

			String fqlOrderId = TestDataManager.randomStr(10);
			BigDecimal amount = new BigDecimal("10.00");

			int skuNum = 1;

			fqlOrderInfoWrapper.insert(fqlOrderId, thirdSkuId, amount, skuNum,
					skuId);
			fqlSyncOrderDetailWrapper.insert(fqlOrderId, 2);

			Logger.comment(skuId);

			FenqileOrderBaseService.generateYmatouOrder();

			FqlSyncOrderDetail actualSyncOrderDetail = fqlSyncOrderDetailWrapper
					.selectByFqlOrderId(fqlOrderId);

			Long orderId = actualSyncOrderDetail.getOrder_id();

			Logger.verifyEquals(null, orderId, "验证ThirdMallOrderId");
			Logger.verifyEquals("分期乐支付价格小于码头支付价格",
					actualSyncOrderDetail.getRemark(), "验证remark");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_FenqileOrderSync_009() {
		Logger.start(true, "商品下架或者被删除，生成分期乐订单失败,验证同步数据");
		try {

			// String ymtProductId = FenqileOrderBaseService
			// .createFenqileProduct(false).getProductIdList().get(0);
			//
			// fenqilesku fenqilesku = FenqileOrderBaseService
			// .getFenqileSkuId(ymtProductId);

			String thirdSkuId = "c4752211";
			String skuId = "MES201911062841851";

			String fqlOrderId = TestDataManager.randomStr(10);
			BigDecimal amount = new BigDecimal("30.00");

			int skuNum = 1;

			fqlOrderInfoWrapper.insert(fqlOrderId, thirdSkuId, amount, skuNum,
					skuId);
			fqlSyncOrderDetailWrapper.insert(fqlOrderId, 2);

			Logger.comment(skuId);

			FenqileOrderBaseService.generateYmatouOrder();

			FqlSyncOrderDetail actualSyncOrderDetail = fqlSyncOrderDetailWrapper
					.selectByFqlOrderId(fqlOrderId);

			Long orderId = actualSyncOrderDetail.getOrder_id();

			Logger.verifyEquals(null, orderId, "验证ThirdMallOrderId");
			Logger.verifyEquals("商品无效", actualSyncOrderDetail.getRemark(),
					"验证remark");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_FenqileOrderSync_010() {
		Logger.start(true, "活动商品，生成分期乐订单,验证同步数据");
		try {

			String ymtProductId = FenqileOrderBaseService
					.createFenqileProduct(true).getProductIdList().get(0);

			fenqilesku fenqilesku = FenqileOrderBaseService
					.getFenqileSkuId(ymtProductId);

			String thirdSkuId = fenqilesku.getYmtSkuId();
			String skuId = fenqilesku.getFqlSkuId();
			fenqilesku.getYmtPrice();

			String fqlOrderId = TestDataManager.randomStr(10);
			BigDecimal amount = fenqilesku.getYmtPrice().add(
					new BigDecimal("10.00"));

			int skuNum = 2;

			fqlOrderInfoWrapper.insert(fqlOrderId, thirdSkuId, amount, skuNum,
					skuId);
			fqlSyncOrderDetailWrapper.insert(fqlOrderId, 2);

			Logger.comment(skuId);

			FenqileOrderBaseService.generateYmatouOrder();

			FqlSyncOrderDetail actualSyncOrderDetail = fqlSyncOrderDetailWrapper
					.selectByFqlOrderId(fqlOrderId);

			long orderId = actualSyncOrderDetail.getOrder_id();

			OrderPo orderPo = orderShardingWapper.selectOrderbyOrderId(orderId);

			OrderDetailPo orderDetailPo = orderShardingWapper
					.selectOrderDetailbyOrderId(orderId).get(0);

			Logger.verifyEquals(true, orderDetailPo.getActivityId() > 0 ? true
					: null, "验证activityId");

			this.verifyDB(orderPo, actualSyncOrderDetail, 2, false, false);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
